<template>
  <div class="login-wrap" @keydown.enter="handleSubmit">
    <div class="content">
      <el-card class="box-card">
        <div slot="header" class="login-title clearfix">
          <el-icon name="star-on"></el-icon>
          <span>欢迎登录</span>
        </div>

        <div class="form-content">
          <el-form
            :model="form"
            :rules="formRules"
            ref="loginForm"
            class="demo-ruleForm"
          >
            <el-form-item prop="userName">
              <el-input
                size="mini"
                v-model="form.userName"
                placeholder="请输入用户名"
              >
                <el-button
                  slot="prepend"
                  icon="el-icon-search"
                  style="padding: 12px 10px"
                ></el-button>
              </el-input>
            </el-form-item>
            <el-form-item prop="password">
              <el-input
                size="mini"
                type="password"
                v-model="form.password"
                auto-complete="off"
                placeholder="请输入密码"
              >
                <el-button
                  slot="prepend"
                  icon="el-icon-search"
                  style="padding: 12px 10px"
                ></el-button>
              </el-input>
            </el-form-item>
            <el-form-item>
              <el-button
                size="mini"
                type="primary"
                @click="handleSubmit"
                style="width: 100%"
                >登录
              </el-button>
            </el-form-item>
          </el-form>
        </div>
        <p class="login-tip">输入任意用户名和密码即可</p>
      </el-card>
    </div>
  </div>
</template>

<script>
import { mapActions } from 'vuex'

export default {
  name: 'Login',
  components: {},
  data() {
    return {
      form: {
        userName: 'admin',
        password: 'admin',
      },
      formRules: {
        userName: [
          { required: true, message: '账号不能为空', trigger: 'blur' },
        ],
        password: [
          { required: true, message: '密码不能为空', trigger: 'blur' },
        ],
      },
    }
  },
  created() {},
  methods: {
    ...mapActions(['handleLogin', 'getUserInfo']),
    handleSubmit() {
      this.$refs.loginForm.validate((valid) => {
        if (valid) {
          this.handleLogin({
            userName: this.form.userName,
            password: this.form.password,
          }).then((res) => {
            this.getUserInfo().then((res) => {
              this.$router.push({
                name: this.$config.homeRouterName,
              })
            })
          })
        }
      })
    },
  },
}
</script>

<style lang="less">
.login-wrap {
  position: relative;
  background-image: url('../../assets/images/home-page/login-bg.png');
  background-size: cover;
  background-position: center;
  width: 100%;
  height: 100%;
  overflow: hidden;

  .content {
    position: absolute;
    right: 160px;
    top: 25%;
  }
  .box-card {
    width: 300px;
  }
  .clearfix:before,
  .clearfix:after {
    display: table;
    content: '';
  }
  .clearfix:after {
    clear: both;
  }
  .form-content {
    padding: 15px;
  }
  .login-title {
    width: 100%;
    font-size: 14px;
    color: #1c2438;
    font-weight: 700;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }
  .login-tip {
    font-size: 10px;
    text-align: center;
    color: #c3c3c3;
  }
}
</style>
